package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.InstallMaterialInfo;
import com.qeeyou.apps.android.craftsman.po.InstallMethodInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;
import com.qeeyou.apps.android.craftsman.view.wheelview.WheelView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstallMethodActivity extends Activity {

    private Button btnCancel;                       //取消按钮
    private Button btnConfirm;                      //确定按钮
    private WheelView wheelView;
    private CustomLoadingDialog mLoadingDialog;
    private List<InstallMethodInfo> methodList;     //方式列表
    private InstallMethodInfo methodInfo;           //安装方式

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intall_method);

        getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        setView();
        setEvent();
    }

    private void setView() {
        wheelView = (WheelView) this.findViewById(R.id.wheelView4InstallMethod);
        btnCancel = (Button) this.findViewById(R.id.btnCancel4InstallMethod);
        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4InstallMethod);
    }

    private void setEvent() {
        if(methodList == null || methodList.size() == 0) {
            if(methodList == null) {
                methodList = new ArrayList<InstallMethodInfo>();
            }
            //填充
            List<String> itemList = new ArrayList<String>();
            itemList.add("不锈钢");
            itemList.add("木质");
            wheelView.setItems(itemList, 0);
            new GetInstallMethodList().execute(new String[] {});
        } else {
            List<String> itemList = new ArrayList<String>();
            for(int i = 0, n = methodList.size(); i < n; i++) {
                InstallMethodInfo methodInfo = methodList.get(i);
                itemList.add(methodInfo.getMethodname());
            }
            wheelView.setItems(itemList, 0);
            wheelView.setOnItemSelectedListener(onItemSelectedListener);
        }
        btnCancel.setOnClickListener(onCancelClickListener);
        btnConfirm.setOnClickListener(onConfirmClickListener);
    }


    //返回按钮
    private View.OnClickListener onCancelClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    InstallMethodActivity.this.finish();
                }
            }, 500);
        }
    };

    //确定按钮
    private View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(methodInfo != null) {
                Intent intent = new Intent();
                intent.putExtra("install_method", methodInfo);
                InstallMethodActivity.this.setResult(RESULT_OK, intent);
            }
            InstallMethodActivity.this.finish();
        }
    };


    private WheelView.OnItemSelectedListener onItemSelectedListener = new WheelView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(int selectedIndex, String item) {
            if(methodList != null) {
                methodInfo = methodList.get(selectedIndex);
            } else {
                methodInfo = null;
            }
        }
    };

    class GetInstallMethodList extends AsyncTask<String, Void, List<InstallMethodInfo>> {

        public GetInstallMethodList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(InstallMethodActivity.this);
            }
            mLoadingDialog.setMessage("正在获取安装材料列表...");
            mLoadingDialog.show();
        }

        @Override
        protected List<InstallMethodInfo> doInBackground(String... params) {
            Map<String, String> paramMap = new HashMap<String, String>();
            List<InstallMethodInfo> methodlList = AppService.getInstance().getInstallMethodList(paramMap);
            return methodlList;
        }

        @Override
        protected void onPostExecute(List<InstallMethodInfo> methodList) {
            super.onPostExecute(methodList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if(methodList != null && !methodList.isEmpty()) {
                InstallMethodActivity.this.methodList.clear();
                InstallMethodActivity.this.methodList.addAll(methodList);
                List<String> titleList = new ArrayList<String>();
                for(int i = 0, n = methodList.size(); i < n; i++) {
                    titleList.add(methodList.get(i).getMethodname());
                }
                wheelView.setItems(titleList, 0);
                methodInfo = methodList.get(0);
                wheelView.setIsLoop(false);
                wheelView.setOnItemSelectedListener(onItemSelectedListener);
            }

        }
    }


}
